<script src="/assets/js/jquery.chosen.min.js"></script>
<%- include('../config/'+ret.key) %>
<script>
    var cid = <%=ret.cid%>;
    var sdk_list = <%-JSON.stringify(ret.sdk)%>;
    let _ret1=<%-JSON.stringify(ret)%>;
    let zone=8;
    if(_ret1.setting){
        zone=_ret1.setting.zone;
    }


    var status_list = {0:'测试[仅开发者账号可见]',1:'正式',2:'下架'};
    var TType_list = {0:"自定义日期",1:"开服第M~N天",2:"注册第M~N天"} 
    var defaultAfterShowForm = function(e,edit){
        $("#sid").css({width:580});
        $("#sort").css({width:100});
        $("#TType").css({width:225});
        $("#STime").css({width:220});
        $("#ETime").css({width:220});

        $("#sid").attr("title","不限制留空,多个服务器','分割'").tooltip({container:'body'});
        $("#sort").attr("title","排序[DESC]大的排在前面").tooltip({container:'body'});
        $("#sid").after($("#sort")).after("&nbsp;");
        $("#tr_sort").remove();

        $("#TType").after($("#ETime")).after(" ~ ").after($("#STime")).after("  ");
        $("#TType").css({height:"37px"}).on("change",TTypeChange);
        $("#tr_STime").remove();
        $("#tr_ETime").remove();


        TTypeChange();
        if(!edit){
            $("#sort").val(0)
        }
        else {
            var status = $("#status").val();
            if (status > 0) {
                $("#status option[value='0']").remove()
                $("#TType").attr("disabled", true);
            }
        }
    }

    function TTypeChange(){
        var TType = $("#TType").val();
        if(TType == 0){
            cosjs.jqgrid.timeMask($("#STime"));
            cosjs.jqgrid.timeMask($("#ETime"));
        }
        else{
            $("#STime").unbind();
            $("#ETime").unbind();
        }
    }

    function timeFormat(val, opt, row){
        if(row.TType == 0 ){
            // console.log("zone:"+zone);
            return cosjs.jqgrid.timeFormatWithZone(val,8);
        }
        else{
            return val||0
        }
    }

    function copyConfigure(id) {
        var data = {id: id,cid:cid};
        var url = GURL(handle,'copy')
        cosjs.post(url, data, function (err,ret) {
            if(err){
                cosjs.alert("复制配置错误",err||ret);
            }
            else{
                $(grid_selector).trigger('reloadGrid');
            }
        })
    }


    var option = {
        colModel : [
            {label: 'id', name: '_id', width: 80,key:true, editable: false,sortable:false, resize:false,search:false,fixed:true,},

            {label: '名称', name: 'name',width: 180, editable: true, sortable:false,hidden:false,resize:true,fixed:false,search:false,
                editrules:{edithidden:true,required:true},
            },
            {label: '渠道限制', name: 'sdk',width: 100, editable: true, sortable:false,hidden:false,resize:false,fixed:true,search:true,
                formatter:'select', edittype:'select', editoptions:{value:sdk_list},
                stype:"select",
                searchoptions:{sopt:['eq'],value:sdk_list}
            },
            {label: '区服限制', name: 'sid',width: 180, editable: true, sortable:false,hidden:true,resize:true,fixed:true,search:false,
                editrules:{edithidden:true}
            },

            {label: '排序', name: 'sort',width: 50, editable: true, sortable:false,hidden:false,resize:true,fixed:true,search:false,
                editrules:{edithidden:true,required:true,integer:true,},
            },
            {label: '状态', name: 'status',width: 50, editable: true, sortable:false,hidden:false,resize:false,fixed:true,search:true,
                formatter:'select', edittype:'select', editoptions:{value:status_list},
                stype:"select",
                searchoptions:{sopt:['eq'],value:status_list}
            },

            {label: '时间格式', name: 'TType',width: 100, editable: true, sortable:false,hidden:false,resize:true,fixed:true,search:false,
                formatter:'select', edittype:'select',editoptions:{value:TType_list},
            },

            {label: '开始时间(Zone'+zone+')', name: 'STime',width: 150, editable: true, sortable:false,hidden:false,resize:true,fixed:true,search:false,
                formatter:timeFormat,
            },
            {label: '结束时间(Zone'+zone+')', name: 'ETime',width: 150, editable: true, sortable:false,hidden:false,resize:true,fixed:true,search:false,
                formatter:timeFormat,
            },
            {label: '活动标识', name: 's_key',width: 150, editable: true, sortable:false,hidden:false,resize:true,fixed:true,search:false,
                formatter:timeFormat,
            },
        ],
        afterShowAddForm : function(e){
            defaultAfterShowForm(e,0);
            if(typeof afterShowForm === 'function'){
                afterShowForm(e,0);
            }
        },
        afterShowEditForm : function(e){
            defaultAfterShowForm(e,1);
            if(typeof afterShowForm === 'function'){
                afterShowForm(e,1);
            }
        },
    }

    function setColModel(key,opts){
        var opts;
        if(typeof arguments[1] === 'object'){
            opts = arguments[1];
        }
        else{
            opts = {};
            opts[arguments[1]] = arguments[2]||'';
        }

        option.colModel.forEach(function(val){
            if(val.name === key){
                $.extend(true,val,opts)
            }
        })
    }

$(document).ready(function () {
    if(typeof beforeCreate === 'function'){
        beforeCreate();
    }
    if(Array.isArray(NewsColModel)){
        option.colModel = option.colModel.concat(NewsColModel)
    }

    cosjs_create_jqgrid(option,{'cid':cid});

    $(grid_selector).jqGrid('navButtonAdd',pager_selector,{
        title:"复制数据",
        caption:"",
        buttonicon:"octicon-repo-pull",
        onClickButton: function(){
            var id=$(grid_selector).jqGrid("getGridParam","selrow");
            if(!id){
                return layer.alert('请先选择一行数据');
            }
            layer.confirm('是否确认复制数据？',
                {  btn: ['取消','确认'] },
                function(){ layer.msg('你放弃了复制数据', {icon: 1}); },
                function(){ copyConfigure(id) }
            );
        }
    })

    if( Array.isArray(NewNavbar)){
        for(var n of NewNavbar){
            $(grid_selector).jqGrid('navButtonAdd',pager_selector,n)
        }
    }

});
</script>

